package org.xl.springbtkafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.xl.springbtkafka.kafka.KafkaReceiver;
import org.xl.springbtkafka.kafka.KafkaSender;

@SpringBootApplication
@EnableScheduling
public class SpringbtKafkaApplication {

    @Autowired
    private KafkaSender kafkaSender;


    public static void main(String[] args) {
        // SpringApplication.run(SpringbtKafkaApplication.class, args);
        SpringApplication sba = new SpringApplication(SpringbtKafkaApplication.class);
        sba.addListeners(new ApplicationStarttingListener());
        sba.run(args);

    }

    @Component
    static class ApplicationStarttingListener implements ApplicationListener<ApplicationStartingEvent> {

        @Override
        public void onApplicationEvent(ApplicationStartingEvent applicationStartedEvent) {
            System.out.println("application starting .....");
        }
    }

    //然后每隔1分钟执行一次
    @Scheduled(fixedRate = 500)
    public void testKafka() {
        kafkaSender.send();
    }

    @Component
    static class AppStartedListener implements ApplicationListener<ApplicationStartedEvent> {

        @Override
        public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
            System.out.println("application started.....");
        }
    }

}
